home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 441 / fplib20 / fabs.c < prev    next >
C/C++ Source or Header  |  1990-11-23  |  1KB  |  54 lines

  1. /* FABS, LDEXP, and FREXP functions for Sozobon C.            */
  2. /* Copyright © David Brooks, 1989 All Rights Reserved            */
  3.  
  4. /* Mustn't allow the standard declaration of fabs()... */
  5. #define MATH_H
  6.  
  7. #include <fplib.h>
  8. #include <errno.h>
  9.  
  10. /* FABS: directly manipulate the sign bit, punning the type.  Actually    */
  11. /* the argument and return are both float.                */
  12.  
  13. unsigned long fabs(a)
  14. unsigned long a;
  15. {    return a & 0xFFFFFF7FL;    }
  16.  
  17. /* LDEXP: again direct manipulation.                    */
  18.  
  19. float ldexp(a, n)
  20. fstruct a;
  21. int n;
  22. {
  23. /* "long" to handle int overflow/underflow... */
  24.     register long exp = (long)(a.sc[3] & EXP_MASK) + n;
  25.  
  26.     if (exp <= 0L)
  27.         return 0L;            /* Underflow */
  28.  
  29.     if (exp > 0x7FL)
  30.     {    errno = ERANGE;            /* Overflow */
  31.         a.ul |= 0xFFFFFF7FL;        /* Preserve sign */
  32.         return a.f;
  33.     }
  34.  
  35.     a.sc[3] = (a.sc[3] & 0x80) | exp;
  36.     return a.f;
  37. }
  38.  
  39. /* FREXP: split into convenient mantissa and exponent            */
  40.  
  41. float frexp(a, ip)
  42. fstruct a;
  43. int *ip;
  44. {    if (a.sc[3] == 0)
  45.         *ip = 0;
  46.     else
  47.     {    *ip = (a.sc[3] & 0x7f) - BIAS;
  48.         a.sc[3] = (a.sc[3] & 0x80) | BIAS;    /* preserve sign,
  49.                                force exponent */
  50.     }
  51.  
  52.     return a.f;
  53. }
  54.